/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.libarae.client.threads;

import java.net.SocketTimeoutException;
import javax.swing.JOptionPane;
import org.libarae.communication.ClientInterface;
import org.libarae.communication.request.FeaturedTabRequest;
import org.libarae.communication.response.FeaturedTabResponse;
import org.libarae.communication.response.Response;

/**
 *
 * @author Ben
 */
public class FeaturedTabThread extends Thread{

    private HasFeaturedTabThread mParent;

    public FeaturedTabThread(HasFeaturedTabThread mParent)
    {
        this.mParent = mParent;
    }

    public void run()
    {
        // Create new Request.
        FeaturedTabRequest r = new FeaturedTabRequest();

        Response response = null;

        // Send Request off to the server.
        try {
            response = ClientInterface.sendRequest(r);
        }
        catch(SocketTimeoutException e)
        {
            JOptionPane.showMessageDialog(null, "Request timed out. Please try again.");
            return;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        if(response == null)
        {
            System.err.println("Error sending to or recieveing from server");
        }

        if(response.getStatus() == Response.STATUS_GOOD)
        {
            FeaturedTabResponse ftr = (FeaturedTabResponse)response;

            mParent.displayBookLists(ftr.getRecentBooks(), ftr.getActiveBooks());
        }

        //TODO: Handle error
    }
}
